package com.yf.controller.ai.manage;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yf.log.annotation.OperationLog;
import com.yf.model.ai.form.AiMaskForm;
import com.yf.model.ai.query.AiMaskPageQuery;
import com.yf.model.ai.vo.AiMaskPageVO;
import com.yf.model.monitor.enums.BusinessTypeEnum;
import com.yf.rate_limiting.annotation.PreventDuplicateSubmit;
import com.yf.result.PageResult;
import com.yf.result.Result;
import com.yf.service.manage.IAiMaskService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * AI 面具-AiMaskController
 * TODO 默认
 *
 * @author: YiFei
 * @since : 2025年7月6日 17:37:48
 */
@Tag(name = "AI 面具")
@RestController
@RequestMapping("ai_mask")
@RequiredArgsConstructor
public class AiMaskController {

    private final IAiMaskService aiMaskService;

    @Operation(summary = "查询AI 面具")
    @OperationLog(title = "查询AI 面具", businessType = BusinessTypeEnum.SEARCH)
    @PreventDuplicateSubmit
    @PreAuthorize("@permission.checker('ai:manage:list')")
    @GetMapping("/page")
    public PageResult<AiMaskPageVO> getAiMaskPage(@ParameterObject @Validated AiMaskPageQuery queryParams) {
        IPage<AiMaskPageVO> result = aiMaskService.getAiMaskPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "AI 面具表单数据")
    @PreventDuplicateSubmit
    @PreAuthorize("@permission.checker('ai:manage:list')")
    @GetMapping("/{id}/form")
    public Result<AiMaskForm> getAiMaskForm(@PathVariable Integer id) {
        AiMaskForm aiMaskForm = aiMaskService.getAiMaskForm(id);
        return Result.judge(aiMaskForm);
    }

    @Operation(summary = "新增AI 面具")
    @OperationLog(title = "新增AI 面具", businessType = BusinessTypeEnum.INSERT)
    @PreventDuplicateSubmit
    @PreAuthorize("@permission.checker('ai:manage:save')")
    @PostMapping
    public Result<Integer> saveAiMask(@RequestBody @Validated AiMaskForm aiMaskForm) {
        Integer result = aiMaskService.saveAiMask(aiMaskForm);
        return Result.judge(result);
    }

    @Operation(summary = "修改AI 面具")
    @OperationLog(title = "修改AI 面具", businessType = BusinessTypeEnum.UPDATE)
    @PreventDuplicateSubmit
    @PreAuthorize("@permission.checker('ai:manage:update')")
    @PutMapping("/{id}")
    public Result<Void> updateAiMask(@Parameter(description = "AI 面具主键") @PathVariable Integer id, @RequestBody @Validated AiMaskForm aiMaskForm) {
        boolean deleted = aiMaskService.updateAiMask(id, aiMaskForm);
        return Result.judge(deleted);
    }

    @Operation(summary = "删除AI 面具")
    @OperationLog(title = "删除AI 面具", businessType = BusinessTypeEnum.DELETE)
    @PreventDuplicateSubmit
    @PreAuthorize("@permission.checker('ai:manage:delete')")
    @DeleteMapping("/{ids}")
    public Result<Void> deleteAiMask(@Parameter(description = "{ids 以 , 分隔") @PathVariable String ids) {
        boolean deleted = aiMaskService.deleteAiMask(Arrays.stream(ids.split(","))
                .map(Integer::parseInt).collect(Collectors.toList()));
        return Result.judge(deleted);
    }

}
